﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DalatCooking.Core;
using DalatCooking.Core.Helper;
public partial class admin_Module_Add_Edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
     protected override void OnInit(EventArgs e)
    {
     
        if (ModuleID != -1)
        {
            GetContentInfo();
        }
        base.OnInit(e);
    }
    void GetContentInfo()
    {
       Module m = ModuleManager.FindById(ModuleID);
        if (m != null)
        {
            txtTitle.Text = m.Title;
            txtDataSource.Text = m.DataSource;
            txtBody.Text = m.Params;

            cbPublished.Checked = m.IsActive;
            txtSortIndex.Text = m.SortIndex.ToString();

            ddlName.SelectedValue = m.Name;
            ddlType.SelectedValue = m.Type;
            ddPosition.SelectedValue = m.Position;
        }
        
    }
    

    public int ModuleID
    {
        get
        {
            if (!String.IsNullOrEmpty(Request.QueryString["ModuleID"]))
            {
                return ConvertObject.ToInt(Request.QueryString["ModuleID"]);
            }
            return -1;
        }
    }

    bool Save()
    {
        Module m = new Module();
        m.ModuleId = -1;
        m.Title = txtTitle.Text;
        m.Name = ddlName.SelectedValue;
        m.Position = ddPosition.SelectedValue;
        m.Type = ddlType.SelectedValue;
        m.IsActive = cbPublished.Checked;
        if (ddlName.SelectedValue.CompareTo("WIDGET_HTML") == 0)
            m.Params = txtBody.Text;
        else m.Params = StringHelper.StripTagsRegexCompiled(txtBody.Text);
        m.DataSource = txtDataSource.Text;

        return ModuleManager.Insert(m);
    }
    bool Update()
    {

        Module m = ModuleManager.FindById(ModuleID);
      
        m.Title = txtTitle.Text;
        m.Name = ddlName.SelectedValue;
        m.Position = ddPosition.SelectedValue;
        m.Type = ddlType.SelectedValue;
        m.IsActive = cbPublished.Checked;
        if (ddlName.SelectedValue.CompareTo("WIDGET_HTML") == 0)
            m.Params = txtBody.Text;
        else m.Params = StringHelper.StripTagsRegexCompiled(txtBody.Text);
        m.SortIndex = ConvertObject.ToInt(txtSortIndex.Text);
        m.DataSource = txtDataSource.Text;

        return ModuleManager.Update(m);
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (ModuleID != -1)
        {
            if (Update())
                Response.Redirect("Default.aspx");
        }
        else
        {
            if (Save())
                Response.Redirect("Default.aspx");
        }

    }
    protected void ddlName_SelectedIndexChanged(object sender, EventArgs e)
    {
        string dataSource = ddlName.SelectedValue;
        switch (dataSource)
        {
            default :
                txtDataSource.Text = "/Widgets/Html.ascx";
                break;
            case "WIDGET_HTML":
                txtDataSource.Text = "/Widgets/Html.ascx";
                break;
            case "WIDGET_POLL":
                txtDataSource.Text = "/Widgets/Poll.ascx";
                break;
            case "WIDGET_WEATHER":
                txtDataSource.Text = "/Widgets/Weather.ascx";
                break;
            case "WIDGET_SHOPPINGCART":
                txtDataSource.Text = "/Widgets/Cart.ascx";
                break;
            case "WIDGET_SEARCH":
                txtDataSource.Text = "/Widgets/Search.ascx";
                break;
            case "WIDGET_LOGIN":
                txtDataSource.Text = "/Widgets/Login.ascx";
                break;
        }
    }
}